package com.sixmac.controller.backend;

import com.sixmac.controller.IndexController;
import com.sixmac.core.Constant;
import com.sixmac.entity.*;
import com.sixmac.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by wangbin on 2015/7/29.
 */
@Controller
@RequestMapping(value = "backend")
public class BackendIndexController {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private MalfunctionService malfunctionService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ModulesService modulesService;

    @Autowired
    private RoleModulesService roleModulesService;

    @RequestMapping(value = "/dashboard")
    public String dashboard(HttpServletRequest request,
                            ModelMap model) {
        // 获取当前登录人信息
        Integer loginUserId = (Integer) request.getSession().getAttribute(Constant.CURRENT_USER_ID);
        SysUser sysUser = sysUserService.getById(loginUserId);

        // 根据当前登录人的角色id获取对应的权限列表
        List<Modules> modulesList = modulesService.findFirstList(sysUser.getRole().getId());
        List<RoleModule> roleModuleList = roleModulesService.findListByRoleId(sysUser.getRole().getId());

        for (Modules module : modulesList) {
            for (RoleModule roleModule : roleModuleList) {
                if (module.getId() == roleModule.getModule().getParentId()) {
                    module.getModuleList().add(roleModule.getModule());
                }
            }
        }

        // 将该用户信息和权限列表放入界面中
        request.getSession().setAttribute("menu_moduleList", modulesList);
        request.getSession().setAttribute("menu_sysUser", sysUser);

        // 只有当前角色是超级管理员时，才查询以下数据
        if (sysUser.getRole().getId() == Constant.ADMIN_ID) {
            // 查询新增APP用户数
            List<AppUser> addUserList = appUserService.findListNew();
            model.addAttribute("appUserNum", addUserList.size());

            // 查询新增微信用户数
            List<WxUser> wxUserList = wxUserService.findListNew();
            model.addAttribute("wxUserNum", wxUserList.size());

            // 查询新增账户数
            List<Account> accountList = accountService.findListNew();
            model.addAttribute("accountNum", accountList.size());

            // 查询新增故障报修数
            List<Malfunction> malfunctionList = malfunctionService.findListNew();
            model.addAttribute("malfunctionNum", malfunctionList.size());

            // 查询新增缴费记录数
            List<Record> recordList = recordService.findListNew();
            model.addAttribute("recordNum", recordList.size());

            // 查询APP用户数
            model.addAttribute("appUserCount", appUserService.findAll().size());

            // 查询微信用户数
            model.addAttribute("wxUserCount", wxUserService.findAll().size());

            // 查询账户数
            model.addAttribute("accountCount", accountService.findAll().size());

            // 查询故障报修总数
            model.addAttribute("malfunctionCount", malfunctionService.findAll().size());

            // 查询待处理故障数
            model.addAttribute("malfunctionCount0", malfunctionService.findListByStatus(Constant.EVENT_STATUS_000).size());

            // 查询处理中故障数
            model.addAttribute("malfunctionCount1", malfunctionService.findListByStatus(Constant.EVENT_STATUS_001).size());

            // 查询已处理故障数
            model.addAttribute("malfunctionCount2", malfunctionService.findListByStatus(Constant.EVENT_STATUS_002).size());

            // 查询今日访问人数
            model.addAttribute("visitCount", IndexController.loginCount);
        }

        return "backend/控制面板";
    }
}
